<template>
  <!-- 注册 -->
  <div class="container mx-auto px-20 mt-2">
    <button class="text-red-500" @click="callback">返回上一级</button>
    <div class="bg-stone-50">
      <el-form
        ref="ruleFormRef"
        :model="registerForm"
        :rules="rules"
        style="max-width: 50%"
        label-width="100px"
        class="py-6 px-20 register-form"
      >
        <el-form-item label="手机号" prop="phone">
          <div class="flex space-x-2">
            <el-input v-model="registerForm.phone" placeholder="请输入手机号">
            </el-input>
            <button class="text-gray-600 bg-gray-200 px-4 whitespace-nowrap">
              获取验证码
            </button>
          </div>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-input
            v-model="registerForm.code"
            placeholder="请输入验证码"
          ></el-input>
        </el-form-item>
        <el-form-item label="店招简称名" prop="shopName">
          <el-input
            v-model="registerForm.shopName"
            placeholder="请输入店招简称名"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户密码" prop="password">
          <el-input
            v-model="registerForm.password"
            placeholder="请输入密码"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPwd">
          <el-input
            v-model="registerForm.confirmPwd"
            placeholder="请输再次入密码"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户地址" prop="address">
          <el-input
            v-model="registerForm.address"
            placeholder="请输入用户地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="QQ号" prop="QQ">
          <el-input v-model="registerForm.QQ" placeholder="请输入QQ"></el-input>
        </el-form-item>

        <el-form-item>
          <el-checkbox-group v-model="registerForm.checkedList">
            <el-checkbox value="1" name="type">
              我已阅读并同意
              <span class="text-red-500">《亿购网网销卖家注册协议》</span>
              <span class="text-red-500">《亿购网隐私协议》</span>
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item>
          <el-button
            class="bg-gray-200 text-gray-400 w-[300px] px-4 py-2"
            @click="register(ruleFormRef)"
          >
            确认注册并登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { RouterLink, useRouter } from "vue-router";
const router = useRouter();
const registerForm = reactive({
  phone: "",
  code: "",
  password: "",
  confirmPwd: "",
  QQ: "",
  shopName: "",
  address: "",
  checkedList: [],
});

const ruleFormRef = ref();
const validatePass = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else {
    if (registerForm.confirmPwd !== "") {
      if (!ruleFormRef.value) return;
      ruleFormRef.value.validateField("confirmPwd");
    }
    callback();
  }
};
const validatePass2 = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请再次输入密码"));
  } else if (value !== registerForm.password) {
    callback(new Error("两次密码不一致!"));
  } else {
    callback();
  }
};
const validatePhone = (rule, value, callback) => {
  const phoneReg = /^1[3-9]\d{9}$/;
  if (!value) {
    callback(new Error("请输入手机号码"));
  } else {
    if (!phoneReg.test(value)) {
      return callback(new Error("请输入正确的手机号码"));
    }
    callback();
  }
};
const rules = reactive({
  phone: [
    { required: true, validator: validatePhone, trigger: "blur" },
    { min: 11, max: 11, message: "手机号长度为11位", trigger: "blur" },
  ],
  code: [
    { required: true, message: "请输入验证码", trigger: "blur" },
    { min: 6, max: 6, message: "验证码长度为6位", trigger: "blur" },
  ],
  password: [
    { required: true, validator: validatePass, trigger: "blur" },
    { min: 6, max: 16, message: "密码长度为6-16位", trigger: "blur" },
  ],
  confirmPwd: [
    { required: true, validator: validatePass2, trigger: "blur" },
    { min: 6, max: 16, message: "密码长度为6-16位", trigger: "blur" },
  ],
  QQ: [
    { required: true, message: "请输入QQ号", trigger: "blur" },
    { min: 5, max: 11, message: "QQ号长度为5-11位", trigger: "blur" },
  ],
  shopName: [{ required: true, message: "请输入店招简称名", trigger: "blur" }],
  address: [{ required: true, message: "请输入地址", trigger: "blur" }],
});

const registerTypeShow = ref("");
const registerIsShow = ref(true);

const callback = () => {
  router.push("/register/step1");
};
const register = async (formEl) => {
  console.log("注册数据：", registerForm);
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      // todo 完成注册

      //注册成功之后
      router.push("/login");
    } else {
      console.log("error submit!", fields);
    }
  });
};
</script>
<style lang="less" scoped>
.register-form {
  .el-input {
    width: 300px;
  }
}
</style>
